package wc

import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}

object Spark01_WordCount {
  def main(args: Array[String]): Unit = {
    val sparkConf: SparkConf = new SparkConf().setMaster("local[*]").setAppName("wordCount")
    val sc = new SparkContext(sparkConf)
    val lines: RDD[String] = sc.textFile("data/test.txt")
    val word: RDD[String] = lines.flatMap(
      item => {
        item.split(" ")
      }
    )
    val wordGroup: RDD[(String, Iterable[String])] = word.groupBy(
      wordD => wordD
    )
    //结构的转换一般用map
    val wordToCount=wordGroup.map{
      case (word,list)=>{
        (word,list.size)
      }
    }
    //采集结果到控制台打印
    val array: Array[(String, Int)] = wordToCount.collect()

    println(array.foreach(println))
    sc.stop()
  }

}
